//
//  UserLogonStateButton.swift
//  SitPosition
//
//  Created by apple on 2018/12/31.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


/**
 * 登录页面上的“登录”、“下一步”、“完成”按钮
 */
class UserLogonStateButton : UIControl {
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryLightTextColor
        label.font = Theme.Font.mediumFont(ofSize: 19)
        label.numberOfLines = 1
        label.textAlignment = .left
        label.lineBreakMode = .byTruncatingTail
        label.adjustsFontSizeToFitWidth = true
        label.isUserInteractionEnabled = true
        return label
    }()
    
    lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "logon_in"))
        iv.contentMode = .scaleAspectFit
        iv.isUserInteractionEnabled = true
        return iv
    }()
    
    private let _title: String?
    
    init(title: String) {
        _title = title
        super.init(frame: CGRect.zero)
        _commonInit()
    }
    
    override init(frame: CGRect) {
        _title = nil
        super.init(frame: frame)
        _commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        _title = nil
        super.init(coder: aDecoder)
        _commonInit()
    }
    
    func _commonInit() {
        addSubviews([
            titleLabel,
            iconView
        ])
        constrain(titleLabel, self) { v, parent in
            v.left == parent.left + 20
            v.right == parent.right - 64
            v.centerY == parent.centerY
        }
        constrain(iconView, self) { v, parent in
            v.width == 25
            v.height == 12
            v.left == parent.right - 60
            v.centerY == parent.centerY
        }
        backgroundColor = Theme.Color.primaryBlueColor
        titleLabel.text = _title
        layer.cornerRadius = 2
        layer.masksToBounds = true
        
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(_didTap))
        tap.numberOfTapsRequired = 1
        tap.numberOfTouchesRequired = 1
        addGestureRecognizer(tap)
    }
    
    @objc func _didTap() {
        sendActions(for: UIControl.Event.touchUpInside)
    }
    
}
